<?


  	defined('SYSPATH') or define('SYSPATH', 'NEED');
	
	if(!class_exists('Template_Controller')){
		class Template_Controller{}
	}

	try {
		include('/home/webnizer/public_html/c-net/system/config/database.php') ;;
	} catch(Exception $e) {
		echo '#Error: ' .$e->getMessage();
	}
	
	
	include_once('/home/webnizer/public_html/c-net/application/controllers/web.php') ;;
 
	$link = mysql_connect($config['default']['connection']['host'], $config['default']['connection']['user'], $config['default']['connection']['pass']);
	if (!$link) {
	    die('Could not connect: ' . mysql_error());
	} 
 
    if (!mysql_select_db($config['default']['connection']['database'])) {
        echo "Unable to select mydbname: " . mysql_error();
        exit;
    }
 
mysql_query("SET NAMES utf8;");
 
    
define('DEBUG', 1);
 
$location = null; #keep track of location/redirects
$cookiearr = null; #store cookies here
    
function read_header($ch, $string)
{
    global $location; #keep track of location/redirects
    global $cookiearr; #store cookies here
    global $ch;
       # ^overrides the function param $ch
       # this is okay because we need to
       # update the global $ch with
       # new cookies
   
    $length = strlen($string);
    if(!strncmp($string, "Location:", 9))
    { #keep track of last redirect
      $location = trim(substr($string, 9, -1));
    }
    if(!strncmp($string, "Set-Cookie:", 11))
    { #get the cookie
      $cookiestr = trim(substr($string, 11, -1));
      $cookie = explode(';', $cookiestr);
      $cookie = explode('=', $cookie[0]);
      $cookiename = trim(array_shift($cookie));
      $cookiearr[$cookiename] = trim(implode('=', $cookie));
    }
    $cookie = "";
    if(trim($string) == "")
    {  #execute only at end of header
    	if (is_array($cookiearr)) {
 
      foreach ($cookiearr as $key=>$value)
      {
        $cookie .= "$key=$value; ";
      }
    	}
     curl_setopt($ch, CURLOPT_COOKIE, $cookie);
     
    }

    return $length;
}



$ch = curl_init();
 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.7) Gecko/2009021912 WTF 5.4 (build 02620) Firefox/3.0.7");
curl_setopt($ch, CURLOPT_REFERER, "http://www.spektr-tv.ru/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$headers = array(
	'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
	'Accept-Language: en-gb,en;q=0.5',
	'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
	'Keep-Alive: 300',
	'Connection: keep-alive'
);
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSLVERSION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');

$min = (DEBUG) ? 0 : rand(10, 120);
 
#echo "Sleep for $min minutes\n\n";
sleep($min);

set_time_limit(0);
 
curl_setopt($ch, CURLOPT_POST, 0);	

curl_setopt($ch, CURLOPT_URL, 'http://www.spektr-tv.ru/news-otr/');

$data = curl_exec($ch); 
curl_setopt($ch, CURLOPT_REFERER, 'http://www.spektr-tv.ru/news-otr/');

preg_match_all("/<p><span class=\"date\">(\d{2})\.(\d{2})\.(\d{4})<\/span>(.*?)<a href=\"(.*?)\"><img width=\"22\" height=\"10\"/ms", $data, $OUT); 
 
unset($OUT[0]);
 
foreach($OUT[1] as $key => $foo){
	
	$date = $OUT[3][$key].'-'.$OUT[2][$key].'-'.$OUT[1][$key]; 
	$title = strip_tags(html_entity_decode(iconv('windows-1251', 'utf-8', $OUT[4][$key])));;
 
	$link = 'http://www.spektr-tv.ru'.trim($OUT[5][$key]);
 
	curl_setopt($ch, CURLOPT_URL, $link);
	
	$min = (DEBUG) ? 0 : rand(10, 120);
 
	sleep($min);

	$data = curl_exec($ch); 
 
	preg_match("/<td valign=\"top\" class=\"pt14 bodytext\">(.*?)<p>/ms", $data, $content); 
	
	$content = str_replace("\n", '<br/>', html_entity_decode($content[1]));
	$content = iconv('windows-1251', 'utf-8', $content);
	 
	$sql = 'insert into bz_news (NW_DATE, NW_TITLE, NW_LINK, NW_TEXT) values(\''.mysql_escape_string($date).'\', \''.mysql_escape_string($title).'\', \''.mysql_escape_string($link).'\', \''.mysql_escape_string($content).'\')';
	
	$result = mysql_query($sql);

	if (!$result) {
		echo "Could not successfully run query ($sql) from DB: " . mysql_error();
		exit;
	}
 
}

print_r($OUT);

?>